
#include "dexsdk.h"
#include "time.h"

#define WHITE 63
#define BLUE 9
#define GREEN 2
#define GRAY 56
#define BROWN 20
#define BLACK 0
#define block_size 20

void drawFloor(int,int);
void drawMan(int,int,int);
void drawWall(int,int);
void reset();
int drawing[20][20];

void reset(){
    int i,j;
	//reset to 0
	for(i=0;i<20;i++){
		for(j=0;j<20;j++){
			drawing[j][i]=0;
		}	
	}
}

void drawFloor(int x, int y){				
    int i,j;
	int ctrI=0,ctrJ=0;
	reset();
	//draw the floor
	for(i=x;i<x+block_size;i++){
		for(j=y;j<y+block_size;j++){	
			write_pixel(i,j,BLACK);			
			ctrJ++;
		}
		ctrJ=0;
		ctrI++;
	}
}


void drawBox(int x, int y){				
    int i,j;
	int ctrI=0,ctrJ=0;
	reset();

	for(i=0;i<20;i++){
		for(j=0;j<20;j++){
			if(i==0 || j==0 || j==19 || j==9 || j==10 || i==9 || i==10 || i==19){
				drawing[j][i]=1;
			}
		}
	}
		
	//draw the floor
	for(i=x;i<x+block_size;i++){
		for(j=y;j<y+block_size;j++){	
			if(drawing[ctrI][ctrJ]==1) write_pixel(i,j,BLACK);
			else write_pixel(i,j,WHITE);
			ctrJ++;
		}
		ctrJ=0;
		ctrI++;
	}
}

void drawBoxOnGoal(int x, int y){				
    int i,j;
    int ctrI=0,ctrJ=0;
	reset();

	for(i=0;i<20;i++){
		for(j=0;j<20;j++){
			if(i==0 || j==0 || j==19 || j==9 || j==10 || i==9 || i==10 || i==19){
				drawing[j][i]=1;
			}
		}
	}
		
	//draw the floor
	for(i=x;i<x+block_size;i++){
		for(j=y;j<y+block_size;j++){	
		   if(drawing[ctrI][ctrJ]==1) write_pixel(i,j,RED);
			else write_pixel(i,j,WHITE);
			
			ctrJ++;
		}	ctrJ=0;
		ctrI++;
	}
}

void drawTarget(int x, int y){				
    int i,j;
    int ctrI=0,ctrJ=0;
	//end of reset to 0	
	reset();
	for(i=0;i<20;i++){
		for(j=0;j<20;j++){
			if(i==0 || j==0 || j==19 || j==9 || j==10 || i==9 || i==10 || i==19){
				drawing[j][i]=1;
			}
		}
	}
	
	//draw the floor
	for(i=x;i<x+block_size;i++){
		for(j=y;j<y+block_size;j++){	
		   if(drawing[ctrI][ctrJ]==1) write_pixel(i,j,RED);
			else write_pixel(i,j,BLACK);			
			ctrJ++;
		}	
		ctrJ=0;
		ctrI++;
	}
}

void drawManUp(int x, int y){
    int i,j;
    int ctrI=0,ctrJ=0;
    reset();
    drawing[2][0]=1;drawing[17][0]=1;
    drawing[7][3]=1;drawing[7][4]=1;drawing[7][5]=1;
    drawing[12][3]=1;drawing[12][4]=1;drawing[12][5]=1;
    drawing[9][7]=1;drawing[10][7]=1;
    drawing[6][7]=1;drawing[13][7]=1;
    drawing[5][8]=1;drawing[14][8]=1;
    drawing[12][12]=1;
    for(i=3;i<6;i++)   for(j=0;j<2;j++)   drawing[i][j]=1;
    for(i=14;i<17;i++) for(j=0;j<2;j++)   drawing[i][j]=1;
    for(i=4;i<6;i++)   for(j=2;j<8;j++)   drawing[i][j]=1;
    for(i=14;i<16;i++) for(j=2;j<8;j++)   drawing[i][j]=1;
    for(i=8;i<12;i++)  for(j=2;j<7;j++)   drawing[i][j]=1;
    for(i=6;i<14;i++)  for(j=8;j<10;j++)  drawing[i][j]=1;
    for(i=8;i<12;i++)  for(j=10;j<13;j++) drawing[i][j]=1;
    for(i=9;i<13;i++)                     drawing[i][13]=1;
    for(i=8;i<14;i++)                     drawing[i][14]=1;
    for(i=11;i<14;i++)                    drawing[i][15]=1;
    for(i=7;i<9;i++)   for(j=15;j<20;j++) drawing[i][j]=1;
    for(i=5;i<7;i++)   for(j=18;j<20;j++) drawing[i][j]=1;

    for(i=x;i<x+block_size;i++){
		for(j=y;j<y+block_size;j++){	
			if(drawing[ctrI][ctrJ]==1) write_pixel(i,j,GRAY);
			else write_pixel(i,j,BLACK);
			ctrJ++;
		}	
		ctrJ=0;
		ctrI++;
	}    
}		


void drawManDown(int x,int y){
	int ctrI=0,ctrJ=0;
	int i,j;
	reset();
	for(i=5;i<9;i++){	drawing[i][0]=1;	drawing[i][1]=1;}
	for(i=7;i<9;i++){	drawing[i][2]=1; 	drawing[i][3]=1;	drawing[i][4]=1;}
	for(i=11;i<14;i++)	drawing[i][4]=1;
	for(i=8;i<14;i++)	drawing[i][5]=1;
	for(i=9;i<13;i++)	drawing[i][6]=1;
	for(i=8;i<13;i++)	drawing[i][7]=1;
	for(i=8;i<12;i++){	drawing[i][8]=1;	drawing[i][9]=1;}
	for(i=6;i<14;i++)	drawing[i][10]=1;
	for(i=5;i<15;i++)	drawing[i][11]=1;
	for(i=4;i<7;i++){	drawing[i][12]=1;	drawing[i+8][12]=1;} 
	for(i=4;i<6;i++)	for(j=13;j<18;j++){	drawing[i][j]=1;	drawing[i+10][j]=1;}
	for(i=3;i<6;i++)	{ drawing[i][18]=1;	drawing[i+11][18]=1;}
	for(i=2;i<6;i++)	{ drawing[i][19]=1;	drawing[i+12][19]=1;}
	for(i=9;i<11;i++)	{ drawing[i][12]=1;	drawing[i][15]=1; }
	for(i=8;i<12;i++)	drawing[i][13]=1;
	for(i=7;i<13;i++){	drawing[i][14]=1;	drawing[i][15]=1;	drawing[i][16]=1;}
	
	for(i=x;i<x+block_size;i++){
		for(j=y;j<y+block_size;j++){	
		   if(drawing[ctrI][ctrJ]==1) write_pixel(i,j,GRAY);
			else write_pixel(i,j,BLACK);
			ctrJ++;
		}	
		ctrJ=0;
		ctrI++;
	}
}	
		
void drawMan(int mode,int x, int y)//i is the mode
{
      
	int ctrI=0,ctrJ=0;
	int i,j;
	//draw human
	reset();
	if(mode==0){
	   //still position
		for(i=0;i<20;i++){
			for(j=0;j<20;j++){
				if(i==0 || j==0 || j==19 || i==19){//border
					drawing[j][i]=1;
				}
				if(j>=7 && j<=12 && i<=14){	//the main body
					drawing[j][i]=1;
				}
				if(i>=13 && i<=16){	//legs
					if(j>=5 && j<=6)	drawing[j][i]=1;//left leg
					if(j>=13 && j<=14)	drawing[j][i]=1;//right leg
				}
				if(i>=17 && i<=18){	//feet
					if(j>=3 && j<=4)	drawing[j][i]=1;//left foot
					if(j>=15 && j<=16)	drawing[j][i]=1;//right foot
				}
				if(i>=7 && i<=8){//arms
					if(j>=3 && j<=6)	drawing[j][i]=1;//left arm
					if(j>=13 && j<=16)	drawing[j][i]=1;//right arm
				}
				if(i>=9 && i<=10){//hands
					if(j>=1 && j<=2)	drawing[j][i]=1;//left hand
					if(j>=17 && j<=18)	drawing[j][i]=1;//right hand
				}
				if(i>=3 && i<=4){//ears
					if(j>=5 && j<=6)	drawing[j][i]=1;//left ear
					if(j>=13 && j<=14)	drawing[j][i]=1;//right ear
				}
			}	
		}
		for(i=x;i<x+block_size;i++){
			for(j=y;j<y+block_size;j++){	
				if(drawing[ctrI][ctrJ]==1) write_pixel(i,j,GRAY);
				else write_pixel(i,j,BLACK);
				ctrJ++;
			}	
			ctrJ=0;
			ctrI++;
		}
		//reset to 0
	}
	if(mode==1){

   //Raise right hand position(viewer's perspective)
	for(i=0;i<20;i++){
		for(j=0;j<20;j++){
			if(i==0 || j==0 || j==19 || i==19){//border
				drawing[j][i]=1;
			}
			if(j>=7 && j<=12 && i<=14){	//the main body
				drawing[j][i]=1;
			}
			if(i>=13 && i<=16){	//legs
				if(j>=13 && j<=14)	drawing[j][i]=1;//right leg
			}
			if(i>=13 && i<=14){	//left leg
				if(j>=3 && j<=6)	drawing[j][i]=1;//left leg
			}
			if(i>=17 && i<=18){	//right foot
				if(j>=15 && j<=16)	drawing[j][i]=1;//right foot
			}
			if(i>=15 && i<=16){	//left foot
				if(j>=1 && j<=2)	drawing[j][i]=1;//left foot
			}
			if(i>=7 && i<=8){//arms
				if(j>=3 && j<=6)	drawing[j][i]=1;//left arm
				if(j>=13 && j<=16)	drawing[j][i]=1;//right arm
			}
			if(i>=9 && i<=10){//left hand
				if(j>=1 && j<=2)	drawing[j][i]=1;//left hand
			}
			if(i>=5 && i<=6){//right hand
				if(j>=17 && j<=18)	drawing[j][i]=1;//right hand
			}
			if(i>=3 && i<=4){//ears
				if(j>=5 && j<=6)	drawing[j][i]=1;//left ear
				if(j>=13 && j<=14)	drawing[j][i]=1;//right ear
			}
		}	
	}
	for(i=x;i<x+block_size;i++){
		for(j=y;j<y+block_size;j++){	
			if(drawing[ctrI][ctrJ]==1) write_pixel(i,j,GRAY);
			else write_pixel(i,j,BLACK);
			ctrJ++;
		}
		ctrJ=0;
		ctrI++;
	}
}

	if(mode==2){//left hand raised
	   //reset to 0
	   //Raise left hand position(viewer's perspective)
		for(i=0;i<20;i++){
			for(j=0;j<20;j++){
				if(i==0 || j==0 || j==19 || i==19){//border
					drawing[j][i]=1;
				}
				if(j>=7 && j<=12 && i<=14){	//the main body
					drawing[j][i]=1;
				}
				if(i>=13 && i<=16){	//left leg
					if(j>=5 && j<=6)	drawing[j][i]=1;//left leg
				}
				if(i>=13 && i<=14){	//right leg
					if(j>=13 && j<=16)	drawing[j][i]=1;//right leg
				}
				if(i>=17 && i<=18){	//left foot
					if(j>=3 && j<=4)	drawing[j][i]=1;//left foot
				}
				if(i>=15 && i<=16){	//right foot
					if(j>=17 && j<=18)	drawing[j][i]=1;//right foot
				}
				if(i>=7 && i<=8){//arms
					if(j>=3 && j<=6)	drawing[j][i]=1;//left arm
					if(j>=13 && j<=16)	drawing[j][i]=1;//right arm
				}
				if(i>=5 && i<=6){//left hand
					if(j>=1 && j<=2)	drawing[j][i]=1;//left hand
				}
				if(i>=9&& i<=10){//right hand
					if(j>=17 && j<=18)	drawing[j][i]=1;//right hand
				}
				if(i>=3 && i<=4){//ears
					if(j>=5 && j<=6)	drawing[j][i]=1;//left ear
					if(j>=13 && j<=14)	drawing[j][i]=1;//right ear
				}				
			}	
		}
		for(i=x;i<x+block_size;i++){
			for(j=y;j<y+block_size;j++){	
				if(drawing[ctrI][ctrJ]==1) write_pixel(i,j,GRAY);
				else write_pixel(i,j,BLACK);
				ctrJ++;
			}	
		ctrJ=0;
		ctrI++;
		}
	}
		
	if(mode==3){
	   //Raise both hands position(viewer's perspective)
		for(i=0;i<20;i++){
			for(j=0;j<20;j++){
				if(i==0 || j==0 || j==19 || i==19){//border
					drawing[j][i]=1;
				}
				if(j>=7 && j<=12 && i<=14){	//the main body
					drawing[j][i]=1;
				}
				if(i>=13 && i<=16){	//legs
					if(j>=5 && j<=6)	drawing[j][i]=1;//left leg
					if(j>=13 && j<=14)	drawing[j][i]=1;//right leg
				}
				if(i>=17 && i<=18){	//feet
					if(j>=3 && j<=4)	drawing[j][i]=1;//left foot
					if(j>=15 && j<=16)	drawing[j][i]=1;//right foot
				}
				if(i>=7 && i<=8){//arms
					if(j>=3 && j<=6)	drawing[j][i]=1;//left arm
					if(j>=13 && j<=16)	drawing[j][i]=1;//right arm
				}
				if(i>=5 && i<=6){//left hand
					if(j>=1 && j<=2)	drawing[j][i]=1;//left hand
					if(j>=17 && j<=18)	drawing[j][i]=1;//right hand
				}
				if(i>=3 && i<=4){//ears
					if(j>=5 && j<=6)	drawing[j][i]=1;//left ear
					if(j>=13 && j<=14)	drawing[j][i]=1;//right ear
				}
			}	
		}
		
		for(i=x;i<x+block_size;i++){
			for(j=y;j<y+block_size;j++){	
				if(drawing[ctrI][ctrJ]==1) write_pixel(i,j,GRAY);
				else write_pixel(i,j,BLACK);
				ctrJ++;
			}
			ctrJ=0;
			ctrI++;
		}
	}
}

void drawWall(int x, int y){
	int i,j;
	int ctrI=0,ctrJ=0;
	reset();
	for(i=0;i<20;i++){
		for(j=0;j<20;j++){
			if(i>=3 && i<=6){//draw first Hbar
				drawing[j][i]=1;
			}
			if(i>=13 && i<=16){//draw second Hbar
				drawing[j][i]=1;
			}
			if(i>=7 && i<=12 && j>=8 && j<=11){	//draw vertical Bar
				drawing[j][i]=1;
			}
			if(i>=0 && i<=2 && (j>=0 && j<=1 || j>=18 && j<=19 )){	//draw excess of first Hbar
				drawing[j][i]=1;
			}
			if(i>=17 && i<=19 && (j>=0 && j<=1 || j>=18 && j<=19 )){	//draw excess of second Hbar
				drawing[j][i]=1;
			}
		}
	}

	for(i=x;i<x+block_size;i++){
		for(j=y;j<y+block_size;j++){	
			if(drawing[ctrI][ctrJ]==1) write_pixel(i,j,BLACK);
			else write_pixel(i,j,BROWN);
			ctrJ++;
		}	
		ctrJ=0;
		ctrI++;
	}
}
